package com.sip.stream.utils.zfy.camera;

import android.hardware.Camera;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.media.AudioAttributes;
import android.media.MediaCodec;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.util.Log;
import android.util.Range;

import com.sip.stream.utils.GBUtils;

import aria.apache.commons.net.ftp.FTPReply;


public abstract class AbstractDeviceMediaStrategy {
    public int WIDTH_480P = 864;
    public int HEGIHT_2K = -1;
    public int HEGIHT_4K = -1;

    public float ACCELEROMETER_VALUE = 6.0f;

    //照片分辨率
    public int CAPTURE_WIDTH = 1920;
    public int CAPTURE_HEGIHT = 1080;

    //扫码宽高
    public int QRSCAN_WIDTH = 1920;
    public int QRSCAN_HEGIHT = 1080;

    //扫码缩放
    public int QRSCAN_ZOOM = 5;

    //前置分辨率
    public int FRONT_WIDTH = 1280;
    public int FRONT_HEGIHT = 720;
    //前置旋转角度
    public int FRONT_ORIENTATION = 90;

    public float FULL_SIZE = 2.0f;

    public int INFRAREDLIGHT_SWITCH_DELAY = FTPReply.UNRECOGNIZED_COMMAND;

    public boolean DUALSTREAM = true;

    public boolean CAMERA2_DUAL_IMAGEREADER = true;

    public boolean Y_EQUAL_ROWSTRIDE = true;

    public boolean I420_IMAGE = false;

    public boolean USE_EGL_PRIVIEW = false;

    public int VIDEO_480_WIDTH = 864;

    public int MAX_CAMERAS_IN_USE = 1;

    public boolean AF_LOCK_FOCUS = false;
    public boolean SUPPORT_WIDE_ANGLE_DISTORTION = false;


    public boolean diffPPIStream() {
        return false;
    }

    public String getCamera2Id(boolean z, boolean z2, boolean z3, CameraManager cameraManager) {
        return null;
    }

    public int getCameraFPSRange(int i, int i2, boolean z) {
        return i;
    }

    public int getSessionConfigurationSessionType(boolean z) {
        return 0;
    }

    public int hal() {
        return 3;
    }

    public boolean isStablization() {
        return true;
    }

    public void setEndOfStream(CameraCaptureSession cameraCaptureSession, CaptureRequest.Builder builder) {
    }

    public void setStablization(boolean z) {
    }

    public Range<Integer> getCamera2FpsRange(int i) {
        return new Range<>(Integer.valueOf(i), Integer.valueOf(i));
    }

    public int[] getPPISelector() {
        return new int[]{1080, 720};
    }

    public int[] getSupportFPS() {
        return new int[]{15, 25, 30};
    }

    public int[] getSupportCaptureDesWidth() {
        return new int[]{7552, 5360, 4624, 4000};
    }

    public int[] getSupportPlatformFPS() {
        return new int[]{5, 15, 30};
    }

    public String[] getVideoMinetype() {
        return new String[]{"H.264/AVC", "H.265/HEVC"};
    }

    public MediaFormat getAVCMediaFormat(int width, int height, int bitrate, int frameRate, int iFrameInterval) {
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat("video/avc", width, height);
        createVideoFormat.setInteger("color-format", 21);
        createVideoFormat.setInteger("bitrate", bitrate);
        createVideoFormat.setInteger("frame-rate", frameRate);
        createVideoFormat.setInteger("i-frame-interval", iFrameInterval);
        return createVideoFormat;
    }

    public MediaFormat getHEVCMediaFormat(int width, int height, int bitrate, int frameRate, int iFrameInterval) {
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat("video/hevc", width, height);
        createVideoFormat.setInteger("color-format", 21);
        createVideoFormat.setInteger("bitrate", bitrate);
        createVideoFormat.setInteger("frame-rate", frameRate);
        createVideoFormat.setInteger("i-frame-interval", iFrameInterval);
        return createVideoFormat;
    }

    public float getMaxZoom(String str, CameraCharacteristics cameraCharacteristics) {
        if (cameraCharacteristics != null) {
            float floatValue = ((Float) cameraCharacteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM)).floatValue();
            Log.d("getMaxZoom", "read SCALER_AVAILABLE_MAX_DIGITAL_ZOOM " + floatValue);
            return floatValue;
        }
        return 8.0f;
    }

    public void setControlSceneMode(boolean z, CaptureRequest.Builder builder, String TAG) {
        if (z) {
            builder.set(CaptureRequest.CONTROL_MODE, 2);
            builder.set(CaptureRequest.CONTROL_SCENE_MODE, 18);
            Log.i(TAG, "CONTROL_SCENE_MODE CONTROL_SCENE_MODE_HDR");
            return;
        }
        builder.set(CaptureRequest.CONTROL_MODE, 1);
        Log.i(TAG, "CONTROL_SCENE_MODE no");
    }

    public int getCamera1Id(boolean z, boolean z2, boolean z3) {
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(i, cameraInfo);
            if (z && cameraInfo.facing == 1) {
                return i;
            }
            if (!z && cameraInfo.facing == 0) {
                return i;
            }
        }
        return -1;
    }

    public String getCamera2Id(boolean z, boolean z2, boolean z3, boolean z4, CameraManager cameraManager) {
        return getCamera2Id(z, z2, z3, cameraManager);
    }

    public AudioAttributes createAudioAttributes() {
        return new AudioAttributes.Builder().setLegacyStreamType(0).setUsage(2).setContentType(1).build();
    }

    public void applyVideoEIS(CaptureRequest.Builder builder, boolean z) {
        builder.set(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE, Integer.valueOf(z ? 1 : 0));
    }


    public MediaCodec createMediaCodec(int width, int height, int bitrate, int frameRate, int iFrameInterval, String streamMineType, String TAG) {
        MediaCodec mediaCodec;
        if (streamMineType.equalsIgnoreCase("video/hevc")) {
            MediaFormat hEVCMediaFormat = GBUtils.getHEVCMediaFormat(width, height, bitrate, frameRate, iFrameInterval);
            if (new MediaCodecList(1).findEncoderForFormat(hEVCMediaFormat) == null) {
                Log.e(TAG, "setUpMediacodec failed: findEncoderForFormat null");
            }
            try {
                mediaCodec = MediaCodec.createEncoderByType("video/hevc");
                try {
                    Log.i(TAG, "mediaCodec name " + mediaCodec.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "createEncoderByType" + e.getLocalizedMessage());
                    mediaCodec.reset();
                    mediaCodec.configure(hEVCMediaFormat, null, null, 1);
                    return mediaCodec;
                }
            } catch (Exception e) {
                mediaCodec = null;
            }
            try {
                mediaCodec.reset();
                mediaCodec.configure(hEVCMediaFormat, null,  null, 1);
            } catch (Exception e) {
                Log.i(TAG, "configure" + e.getLocalizedMessage());
                e.printStackTrace();
            }
        } else {
            mediaCodec = null;
        }
        if (mediaCodec == null) {
            MediaFormat aVCMediaFormat = GBUtils.getAVCMediaFormat(width, height, bitrate, frameRate, iFrameInterval);
            if (new MediaCodecList(1).findEncoderForFormat(aVCMediaFormat) == null) {
                Log.e(TAG, "setUpMediacodec failed: findEncoderForFormat null");
            }
            try {
                mediaCodec = MediaCodec.createEncoderByType("video/avc");
                Log.i(TAG, "mediaCodec name " + mediaCodec.getName());
            } catch (Exception e) {
                Log.i(TAG, "createEncoderByType" + e.getLocalizedMessage());
                e.printStackTrace();
            }
            try {
                mediaCodec.reset();
                mediaCodec.configure(aVCMediaFormat, null, null, 1);
            } catch (MediaCodec.CodecException e5) {
                Log.i(TAG, "configure" + e5.getLocalizedMessage());
                e5.printStackTrace();
            }
        }
        return mediaCodec;
    }
}
